package zero.tech.games.gate.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.common.Code;
import zero.tech.games.common.FrozenUtil;
import zero.tech.games.common.GameConfig;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.games.ClubGameRule;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.gate.hot.ClubUtil;
import zero.tech.games.gate.util.PipeiUtil;
import zero.tech.wsnet.data.CheckPipeiQueue;
import zero.tech.wsnet.data.CheckUserLastRoom;

@RestController
public class PipeiController extends BaseController{
	@Autowired
	private ClubUtil clubUtil;
	@Autowired
	private FrozenUtil frozenUtil;
	@Autowired
	private PipeiUtil pipeiUtil;
	
	@CheckUserLastRoom
	@CheckPipeiQueue
	@RequestMapping("/add-pipei-queue")
	public Resp addPipeiQueue(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		int uid=simpleUser.getUid();
		int rule_id=Integer.valueOf(map.get("rule_id").toString());
		ClubGameRule clubGameRule=clubUtil.loadHotClubGameRule(rule_id);
		if(clubGameRule==null) {
			return Resp.build(Code.PARAM_ERROR, "玩法不存在");
		}
		int club_id=clubGameRule.getClub_id();
		Club club=clubUtil.loadHotClub(club_id);
		if(club==null) {
			return Resp.build(Code.CLUB_NOT_EXSIT, "不存在茶馆");
		}
		ClubMember clubMember=clubUtil.getClubMember(uid,club_id);
		if(clubMember==null) {
			return Resp.build(Code.PARAM_ERROR, "不属于该茶馆");
		}
		boolean frozen=frozenUtil.infrozenlist(club_id, uid);
		if(frozen) {
			return Resp.build(Code.HAVE_BEING_FROZEN, "被冻结了不能参与游戏");
		}
		
		//检查玩家的钱
		GameConfig gameConfig= JSONObject.parseObject(clubGameRule.getConfig_str(),GameConfig.class);
		long admission_fee=gameConfig.getAdmission_fee();
		if(clubMember.getTea()<admission_fee) {
			return Resp.build(Code.MONEY_NOT_ENOUGH, "入场费不够");
		}
		return pipeiUtil.addToQueue(clubGameRule,simpleUser);
	}
	
	
	@RequestMapping("/remove-pipei-queue")
	public Resp removePipeiQueue(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		return pipeiUtil.removeFromQueue(simpleUser);
	}
	
	
	
	@RequestMapping("/get-pipei-queue-size")
	public Resp getPipeiQueueSize(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		int rule_id=Integer.valueOf(map.get("rule_id").toString());
		return pipeiUtil.getPipeiQueueSize(simpleUser,rule_id);
	}
	
	@RequestMapping("/get-pipei-queue-size-batch")
	public Resp getPipeiQueueSizeBatch(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		SimpleUser simpleUser=super.getLoginUser(request);
		List<Integer> rule_ids=JSONObject.parseObject(map.get("rule_ids").toString(),new TypeReference<List<Integer>>() {});
		return pipeiUtil.getPipeiQueueSize(simpleUser,rule_ids);
	}
}
